import org.junit.Test;

import java.io.*;
import java.util.Properties;

/**
 * @Author talent_dog
 * @Date 2024/1/16 12:24
 * @Version 1.0
 */
public class HomeWork03{
    @Test
    public void work01() throws IOException {
        File file = new File("C:\\Users\\23652\\Desktop\\bs\\test\\mytemp\\dog.properties");
        if (file.exists()){
            System.out.println("文件存在");
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            bw.write("name=tome");
            bw.newLine();
            bw.write("age=10");
            bw.newLine();
            bw.write("color=red");
            bw.newLine();
            bw.close();
        }else {
            System.out.println("文件不存在");
            file.createNewFile();
        }
    }
    @Test
    public void work02() throws IOException, ClassNotFoundException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("C:\\Users\\23652\\Desktop\\bs\\test\\mytemp\\dog.properties"));
        Dog dog = new Dog();
        dog.setName(properties.getProperty("name"));
        dog.setAge(Integer.parseInt(properties.getProperty("age")));
        dog.setColor(properties.getProperty("color"));
        System.out.println(dog);
        //序列化到文件
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("C:\\Users\\23652\\Desktop\\bs\\test\\mytemp\\dog.ser"));
        oos.writeObject(dog);
        oos.close();

        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("C:\\Users\\23652\\Desktop\\bs\\test\\mytemp\\dog.ser"));
        Dog dog1=(Dog) ois.readObject();
        System.out.println(dog1);
    }
}
